package com.example.study.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jiange
 * @create 2021-05-09-0:07
 */
@Controller
public class MyController {
    @GetMapping("/vip")
    public String vip(){
        System.out.println("vip");
        return "vip";
    }
    @GetMapping("/login")
    public String login(){
        return "login";
    }
    @GetMapping("/form")
    public String form(@RequestParam("jian") String name, String pass, Model model){
        UsernamePasswordToken token = new UsernamePasswordToken(name, pass);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return "redirect:/vip";
        } catch (UnknownAccountException e) {
            model.addAttribute("mes","没有这个人");
            return "/login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("mes","密码不正确");
            return "/login";
        }
    }
}
